<style>
    #article_info{
        font-size: 25px;font-weight: 900;cursor: pointer; color: #4a7eb3;
    }
    #article_info:hover{
        color: #98bce0;
    }

    .article-layer{
        padding: 30px;
        font-size: 18px;
        color:#979797;
    }
</style>
<div class="fly-panel" style="padding: 10px 20px 0 20px;">
    <!--<div class="fly-none">没有权限</div>-->
    <div class="layui-form">
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title">
                <li class="layui-this">编写文章</li>
                <li>发布设置</li>
                <li>切换编辑器</li>
            </ul>
            <div class="layui-form  layui-tab-content" style="padding: 15px 0 5px 0;">
                <div class="layui-row layui-col-space15 layui-form-item layui-form-pane">
                
                    <div class="layui-col-md9">
                        <input type="text" placeholder="标题 (不超过50个文字)" id="article_title" style="font-size: 20px;" name="title" required lay-verify="required"
                                autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-col-md3" style="line-height: 38px;">
                        
                        <!-- <input type="checkbox" name="" title="自动保存" checked> -->
                        <!-- <div class="layui-btn layui-btn-primary">已发布</div> -->
                        <i class="layui-icon layui-icon-about" id="article_info"></i>
                        <div class="layui-inline" style="float: right;">
                            <button class="layui-btn layui-btn-sm" id="save-btn" lay-filter="save-btn" title="仅保存前台页面不会更新">保存</button>
                            <button class="layui-btn layui-btn-sm" lay-filter="save-release-btn" id="save-release-btn" title="保存并发布到前台" lay-submit>发布</button>
                        </div>
                        
                            
                        
                        <!-- <span id="save-status-lable"></span> -->
                        <!-- <input type="checkbox"  lay-filter="auto_save" title="自动保存" lay-skin="primary"> -->
                        <!-- <button class="layui-btn layui-btn-sm" data-auto_save="0" lay-filter="*" lay-submit>自动保存</button> -->
                    </div>
                </div>
                
                <div class="layui-tab-item  layui-form-pane layui-show">
                        
                        <div class="layui-form-item">
                            <div class="layui-tab-item layui-show">
                                <iframe id="editor" src=""   style="width: 100%;" frameborder="0"></iframe>
                            </div>
                        </div>
                        <!-- <div class="layui-form-item">
                                    <button class="layui-btn" lay-filter="*" lay-submit>立即发布</button>
                                </div> -->
                </div>
                <div class="layui-tab-item">
                    <hr>
                    <form action="" method="post" class="layui-form" lay-filter="article-config">
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">文章简介</label>
                            <div class="layui-input-block">
                                <textarea id="description" name="description"  placeholder="（选填）请输入文章简介" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">公开状态</label>
                            <div class="layui-input-block" >
                                <input type="radio" name="status" lay-filter="open-status" value="0" title="私密">
                                <input type="radio" name="status" lay-filter="open-status" value="1" title="公开" checked>
                            </div>
                        </div>
                        <!-- <div class="layui-form-item">
                            <label class="layui-form-label">保存即发布</label>
                            <div class="layui-input-block">
                                <input type="radio" name="auto_release" value="0" title="否" checked>
                                <input type="radio" name="auto_release" value="1" title="是">
                            </div>
                        </div> -->

                        <div class="layui-form-item">
                            <label class="layui-form-label" title="收录到我的专栏">收录到专栏</label>
                            <div class="layui-input-block">
                                <div id="anthology_bind" class=""></div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label" title="推送到公开专栏">推送到公开专栏</label>
                            <div class="layui-input-block">
                                <div id="anthology_bind2" class=""></div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">添加标签</label>
                            <div class="layui-input-block">
                                <div id="tag_bind" class=""></div>
                            </div>
                        </div>
                    </form>

                </div>

                <div class="layui-form layui-form-pane layui-tab-item">
                    <blockquote class="layui-elem-quote">仅对本文章生效。如果文章已编写内容，切换后可能导致文章渲染或显示不正常，请谨慎！</blockquote>
                    <form class="layui-form" lay-filter="form-editor-config">
                        
                        <div class="layui-form-item">
                            <label class="layui-form-label">编辑器</label>
                            <div class="layui-input-block">
                                <select name="editor" lay-verify="required">
                                    <option value="2" checked>富文本编辑器（适合大部分用户，上手简单）</option>
                                    <option value="1">Markdown 编辑器（适合了解markdown语法用户）</option>
                                </select>
                            </div>
                        </div>
                      
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="submit-editor-config">确定</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            
            
        </div>
    </div>
</div>

<script type="text/html" id="upload_file_tpl">
    <div class="layui-form" style="padding: 10px;">
        <div class="layui-form-item">
            <label class="layui-form-label">图片地址</label>
            <div class="layui-input-inline">
                <input type="text"  required lay-verify="required" id="upload_file_url" placeholder="图片地址..." 
                    class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <div class="layui-btn layui-btn-sm layui-btn-primary" id="upload_file_btn">本地上传</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片描述</label>
            <div class="layui-input-inline">
                <input type="text" name="text" id="upload_file_alt" placeholder="图片的描述..." 
                    class="layui-input">
            </div>
            
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片链接</label>
            <div class="layui-input-inline">
                <input type="text" name="text" id="upload_file_click_url" placeholder="点击图片跳转的链接" 
                    class="layui-input">
            </div>
        
        </div>
    </div>
</script>

<script>

    // 给子页面开放接口
    function getApp() {
        return app
    }
    layui.use(["jquery","form","upload"],function(){
        var $=layui.$,form=layui.form,upload=layui.upload

        var editor={}
        var tag_select, anthology_select2, anthology_select

        var article_id = Number(app.parseUrl().params.article_id);
        var urlEditor= Number(app.parseUrl().params.editor);
        var defineEditor=["meditor","wang_editor"]
        var auto_release=false;
        var oldSaveContent="";// 每次保存进行更新（仅限内容）
        var isSave=true;// 是否保存
        var articleEditor=2;// 1.md 2.富文本
        var uploadConfig={};// 上传的配置
        var siteTitle="创作"
        var articleInfo={};// 文章详情

        // 获取用户的编辑器配置 编辑器邮箱优先级：文章保存的编辑器 > 浏览器url配置编辑器 > 用户默认编辑器
        app.base.ajaxGet("/api/personal/getUserConfig",function(userConfig){
            
            var articleEditorName= "";
            // 如果get参数中定义使用的编辑器，强制浏览器中所定义的
            if (urlEditor) {
                articleEditorName = defineEditor[urlEditor - 1];
                articleEditor = urlEditor
            }else{
                articleEditorName = defineEditor[userConfig.editor - 1];
                articleEditor = userConfig.editor
            }

            form.val("form-editor-config", {"editor": articleEditor})
            
            // 编辑文章
            if (article_id) {
                
                // 获取配置
                app.base.ajaxPost("/api/personal/getArticleInfoAndConfig", { article_id: article_id }, function (articleConfig) {
                    articleInfo= articleConfig
                    // console.log("文章详情",articleInfo)
                    articleEditorName = defineEditor[articleConfig.editor - 1];
                    articleEditor = articleConfig.editor
                    $("#editor").attr("src", "/profile/pages/writing_editor/editors/" + articleEditorName + ".html")
                    var iframeEditor = $("#editor");
                    
                    form.val("article-config", articleConfig)
                    form.val("form-editor-config", articleConfig)
                    siteTitle ="编写文章 - " +articleConfig.title
                    
                    $("#article_title").val(articleConfig.title)
                    iframeEditor.load(function () {
                        editor = iframeEditor[0].contentWindow.$E;
                        editor.uploadFile = function () {
                            uploadFileLayer()
                        }
                        editor.setContent(articleConfig.content)
                        editor.app=app
                        // console.log("加载完成",editor.getContent())
                        // console.log("加载完成1", articleConfig.content)
                        oldSaveContent = articleConfig.content// 获取初始内容进行修改比对
                        // oldSaveContent = editor.getContent()// 获取初始内容进行修改比对
                        listenSaveKey()
                        open_timing_content()
                    })


                    var anthologysIds = objectToArrayById(articleConfig.anthologys)
                    var tags = objectToArrayById(articleConfig.tags)
                    renderMyAnthology(anthologysIds)
                    renderOpenAnthology(anthologysIds)
                    renderTag(tags)
                    
                })
            }else{
                
                $("#editor").attr("src", "/profile/pages/writing_editor/editors/" + articleEditorName + ".html")
                var iframeEditor = $("#editor");
                iframeEditor.load(function () {
                    editor = iframeEditor[0].contentWindow.$E;
                    editor.uploadFile = function () {
                        uploadFileLayer()
                    }
                    editor.app = app
                    // console.log("加载完成", editor.getContent())
                    oldSaveContent = editor.getContent()// 获取初始内容进行修改比对,由于编辑器为空，可能存在初始样式，故读取一次编辑器的
                    listenSaveKey()
                    open_timing_content()
                })
                // var iframeEditor = $("#editor");
                renderMyAnthology()
                renderOpenAnthology()
                renderTag()
                
            }

            
            // iframeEditor.load(function () {
            //     editor = iframeEditor[0].contentWindow.$E;
            //     editor.uploadFile = function () {
            //         uploadFileLayer()
            //     }
            //     if (article_id) {
            //         // 读取配置
            //         app.base.ajaxPost("/api/personal/getArticleInfoAndConfig", { article_id: article_id }, function (data) {
            //             oldSaveContent = data.content
            //             form.val("article-config", data)
            //             $("#article_title").val(data.title)
            //             editor.setContent(data.content)


            //             var anthologysIds = objectToArrayById(data.anthologys)
            //             var tags = objectToArrayById(data.tags)
            //             renderMyAnthology(anthologysIds)
            //             renderOpenAnthology(anthologysIds)
            //             renderTag(tags)
            //         })
            //     } else {
            //         renderMyAnthology()
            //         renderOpenAnthology()
            //         renderTag()
            //     }

            //     // 监听快捷键
            //     $("body iframe").contents().bind("keydown", save_keydown);
            //     $(document).bind("keydown", save_keydown);

            // })


            layui.jquery("#editor").css("height", layui.jquery(document).height() - 250 + "px")
            layui.form.render()

            run_listen()

        })

        
        
        
        

        // 公开专栏 后端搜索版本
        // xmSelect.render({
        //     el: '#anthology_bind2',
        //     filterable: true,
        //     tips: '（选填）最多只能推送到2个公开专栏，如果公开专栏需要审核，通过后才能被收录成功',
        //     max: 2,
        //     maxMethod(seles, item) {
        //         layer.msg("最多只能选择2个")
        //     },
        //     remoteSearch: true,
        //     remoteMethod: function (val, cb, show) {
                
        //         // //这里模拟3s后返回数据
        //         // setTimeout(function () {
        //         //     //需要回传一个数组
        //         //     cb([
        //         //         { name: '水果' + val, value: val + 1 },
        //         //         { name: '蔬菜' + val, value: val + 2, selected: true },
        //         //         { name: '桌子' + val, value: val + 3 },
        //         //         { name: '北京' + val, value: val + 4 },
        //         //     ])
        //         // }, 3000)
        //     },
        //     data: []
        // })

   
      

        // 对象数组转数组
        function objectToArrayById(objArr){
            var arr=[];
            for (let i = 0; i < objArr.length; i++) {
                const element = objArr[i];
                arr.push(element.id)
            }
            return arr
        }

        // 渲染我的专栏
        function renderMyAnthology(selectedAnthologysIds){
            var selectedAnthologysIds= selectedAnthologysIds || [];
            // 我的专栏
            app.base.ajaxGet("/api/personal/getAnthologyList", function (data) {
                var retrunData = []
                for (let i = 0; i < data.list.length; i++) {
                    const element = data.list[i];
                    if (inArray(element.id, selectedAnthologysIds)) {
                        retrunData.push({ name: element.title, value: element.id, selected: true })
                    } else {
                        retrunData.push({ name: element.title, value: element.id })
                    }
                }
                anthology_select=xmSelect.render({
                    el: '#anthology_bind',
                    tips: '（选填）最多只能选择1个',
                    max: 1,
                    theme: {
                        color: '#4a7eb3',
                    },
                    filterable: true,
                    maxMethod(seles, item) {
                        layer.msg("最多只能选择1个")
                    },
                    data: retrunData
                })
            })
        }

        // 渲染公开专栏
        function renderOpenAnthology(selectedAnthologysIds) {
            var selectedAnthologysIds = selectedAnthologysIds || [];
            app.base.ajaxGet("/api/global/get/anthologyList?accept_article=1,3&golbal_open=1&exclude_myself=1", function (data) {
                var retrunData = []
                for (let i = 0; i < data.list.length; i++) {
                    const element = data.list[i];
                    element.title+=" ["+ element.name+"]"
                    if (inArray(element.id, selectedAnthologysIds)) {
                        retrunData.push({ name: element.title, value: element.id, selected: true })
                    } else {
                        // 需要审核的专栏
                        if(element.accept_article==3){
                            element.title+=" (需审核通过后展示)"
                        }
                        retrunData.push({ name: element.title, value: element.id })
                    }
                }
                anthology_select2=xmSelect.render({
                    el: '#anthology_bind2',
                    tips: '（选填）最多只能选择2个。此项仅在发布时生效！',
                    max: 2,
                    theme: {
                        color: '#4a7eb3',
                    },
                    filterable: true,
                    maxMethod(seles, item) {
                        layer.msg("最多只能选择2个")
                    },
                    data: retrunData
                })
            })
        }

        // 渲染标签
        function renderTag(tags){
            var tags = tags || [];
            tag_select=xmSelect.render({
                el: '#tag_bind',
                tips: '（选填）最多只能添加5个标签',
                searchTips: '请输入标签名称',
                max: 5,
                theme: {
                    color: '#4a7eb3',
                },
                filterable: true,
                remoteSearch: true,
                remoteMethod: function (val, cb, show) {
                    app.base.ajaxPost("/api/personal/getTagList", { "title": val }, function (data) {
                        var retrunData = []
                        for (let i = 0; i < data.list.length; i++) {
                            const element = data.list[i];
                            if (inArray(element.id, tags)) {
                                retrunData.push({ name: element.title, value: element.id, selected: true })
                            } else {
                                retrunData.push({ name: element.title, value: element.id })
                            }
                        }

                        // 库中没有则新建
                        if (retrunData.length == 0) {
                            retrunData.push({ name: val, value: 0, create: true })
                        }

                        cb(retrunData)
                    })

                },
                data: []
            })
        }

        // 是否在数组内
        function inArray(search, array) {
            // undefined
            for (var i in array) {
                // undefined
                if (array[i] == search) {
                    // undefined
                    return true;
                }
            }
            return false
        }

        // 获得文章配置
        function getConfig(){
            var cfg = form.val("article-config")
            var config={};
            auto_release=cfg.auto_release// 判断是否自动发布
            cfg.anthologys= anthology_select.getValue("value")

            
            cfg.anthologys.push.apply(cfg.anthologys, anthology_select2.getValue("value"))
            var tags = tag_select.getValue()
            config.tags=[];
            config.new_tags = [];
            for (let i = 0; i < tags.length; i++) {
                const element = tags[i];
                if(element.value!=0){
                    config.tags.push(element.value)
                }else{
                    config.new_tags.push(element.name)
                }
                
            }
            config.status= Number(cfg.status)
            config.auto_release = Number(cfg.auto_release)
            config.description = cfg.description
            config.editor =articleEditor
            config.anthologys = cfg.anthologys
            return config
        }
        
        function setSiteTitle(c){
            $("title").html(c)
        }

        // 监听触发保存快捷键
        function listenSaveKey(){
            // 监听快捷键
            $("body iframe").contents().bind("keydown", save_keydown);
            $(document).bind("keydown", save_keydown);
        }

        // 未保存关闭拦截
        
        // function onbeforeunload_handler() {
        //     var warning = "退出吗?";
        //     return warning;
        // }

        // function onbeforeunload(status){
        //     if(status==true){
        //         window.onbeforeunload = onbeforeunload_handler;
        //     }else{
        //         window.onbeforeunload = false;
        //     }
            
        // }
        

        function run_listen(){

            // 监听文章详情按钮被单击
            $("#article_info").click(function(){
                var html="<div class='article-layer'>"+
                    "<p>访问数：" + (articleInfo.visit? articleInfo.visit:0 )+ "</p>" + 
                    "<p>公开状态："+(articleInfo.status==1?"公开":"私密")+"</p><hr>" + 
                    "<p style='font-size:13px;'>首次发布：" + (articleInfo.release_time ? articleInfo.release_time : "未发布") + "</p>" +
                    "<p style='font-size:13px;'>更新发布：" + (articleInfo.release_update_time ? articleInfo.release_update_time : "未发布") + "</p><hr>"+
                    // "<p style='font-size:13px;'>创建时间："+ articleInfo.create_time +"</p>" + 
                    "<p style='font-size:13px;'>最后修改：" + (articleInfo.update_time? articleInfo.update_time:"") +"</p></div>" ;
                    
                
                app.base.layer({
                    type: 1,
                    shade: 0.5,
                    shadeClose: true,
                    title: "文章信息",
                    area: ['300px'],
                    content: html,
                })  
            })

            // 保存按钮点击
            $("#save-btn").click(function () {
                save_article()
            })

            // 保存并发布
            $("#save-release-btn").click(function () {
                layer.confirm('请确定是否要发布？', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    save_article(true,function(res){
                        if(res==true){
                            layer.close(index)
                            layer.msg("已成功发布")
                        }
                    })
                });
                
            })

            // 切换编辑器的保存
            form.on('submit(submit-editor-config)', function (data) {
                layer.confirm('如果文章已编写内容，切换后可能导致文章渲染或显示不正常，请谨慎修改', {
                    btn: ['执意修改', '取消'] //按钮
                }, function () {
                    var reload = true
                    if (!article_id) {
                        reload = false
                    }
                    articleEditor = Number(data.field.editor)
                    save_article()
                    if (reload) {
                        if(app.parseUrl().params.article_id){
                            location.reload()
                        }else{
                            location.href = "/profile/writing.html/#/editor?article_id=" + article_id;
                        }
                        layer.msg("切换成功，如果没有跳转，请手动刷新")
                    } else {
                        location.href = "/profile/writing.html/#/editor?editor=" + data.field.editor;
                    }
                });
                
                
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });

            // 开启自动保存
            // form.on('checkbox(auto_save)', function (data) {
            //     // layer.msg("已开启自动保存")
            //     // console.log(data.elem); //得到checkbox原始DOM对象
            //     // console.log(data.elem.checked); //是否被选中，true或者false
            //     if(data.elem.checked){
            //         // 触发开启
            //     }else{
            //         // 触发关闭
            //     }
            //     // console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            //     // console.log(data.othis); //得到美化后的DOM对象
            // });        
            // open_timing_save()
            

            
        }

        // 保存文章
        // 参数 是否保存渲染
        function save_article(isRelease,callback) {
            $("#save-btn").html('<i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i>')
            var data = {}
            data.title = $("#article_title").val()
            data.content = editor.getContent()
            if(data.title=="" || data.content==""){
                layer.msg("标题和内容不能为空")
                return
            }
            oldSaveContent = data.content
            var config = getConfig()
            for (const k in config) {
                if (Object.hasOwnProperty.call(config, k)) {
                    const element = config[k];
                    data[k] = element
                }
            }

            // 保存渲染内容
            if(isRelease){
                data.content_render = editor.getContentRender()
                // if (data.content_render == "" ) {
                //     layer.msg("发布内容不能为空")
                //     return
                // }
            }
            
            if (article_id) {
                // 更新
                data.id = article_id
                app.base.ajaxPost("/api/personal/saveArticle", data, function (res) {
                    articleInfo.update_time = app.base.getDateTime()
                    articleInfo.create_time = app.base.getDateTime()
                    articleInfo.status = data.status
                    // 发布
                    if(isRelease){
                        if(callback){
                            // articleInfo.release_time = app.base.getDateTime() // 非首次不更改
                            articleInfo.release_update_time = app.base.getDateTime()
                            callback(true)
                        }
                    }
                    isSave=true
                    
                    $("#save-btn").html("已保存")
                    setTimeout(function () {
                        $("#save-btn").html("保存")
                    }, 1000);
                })
            } else {
                // 新增文章
                if(data.title && data.content ){
                    app.base.ajaxPost("/api/personal/saveArticle", data, function (res) {
                        articleInfo.update_time = app.base.getDateTime()
                        articleInfo.create_time = app.base.getDateTime()
                        articleInfo.status = data.status
                        // 发布
                        if (isRelease) {
                            if (callback) {
                                articleInfo.release_time = app.base.getDateTime()
                                articleInfo.release_update_time = app.base.getDateTime()
                                callback(true)
                            }
                        }
                        isSave = true
                        $("#save-btn").html("保存")
                        setTimeout(function () {
                            $("#save-status-lable").html("")
                        }, 1000);
                        article_id = res.id
                    })
                }else{
                    // console.log("添加的时候不存")
                }
                // 否则不保存
            }
        }

        // 按键保存
        function save_keydown(e) {

            if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
               
                setTimeout(function () {
                    save_article()

                }, 100);
                return false;
            }
        }

        // 定时保存 60秒存一次
        // function open_timing_save(params) {
        //     // 暂时取消自动保存
        //     // setInterval(function () {
        //     //     // 判断与旧的内容是否一致，不一致进行保存
        //     //     if (oldSaveContent != editor.getContent()) {
        //     //         save_article()
        //     //     }
        //     // }, 1000*60);
        // }

        // 定时检测内容是否一致(2s)
        function open_timing_content(params) {
            setInterval(function () {
                // 判断与旧的内容是否一致，不一致进行保存
                // console.log(oldSaveContent);
                // console.log("-------------");
                // console.log(editor.getContent());
                siteTitle="编写文章 - "+ $("#article_title").val()
                if (oldSaveContent != editor.getContent()) {
                    isSave=false
                    $("#save-btn").html("*保存")
                    setSiteTitle("*"+siteTitle)
                }else{
                    $("#save-btn").html("保存")
                    isSave=true
                    setSiteTitle(siteTitle)
                }
            }, 1000 * 2);
        }
        
        // 上传文件弹窗
        function uploadFileLayer(){
            // 弹窗按钮
            layerButtons = [
                {
                    title: '确定',
                    type: 'primary',// warm,danger,primary
                    onClick: function (layer_index) {
                        layer.close(layer_index)

                        var url = $("#upload_file_url").val();
                        var alt = $("#upload_file_alt").val();
                        var link = $("#upload_file_click_url").val();

                        if (url === "") {
                            alert(imageLang.imageURLEmpty);
                            return false;
                        }

                        var altAttr = (alt !== "") ? " \"" + alt + "\"" : "";

                        if (link === "" || link === "http://") {
                            editor.insertValue("![" + alt + "](" + url + altAttr + ")");
                        }
                        else {
                            editor.insertValue("[![" + alt + "](" + url + altAttr + ")](" + link + altAttr + ")");
                        }
                        return false;//阻止默认事件
                    }
                },
            ];
            app.base.layer({
                type: 1,
                shade: 0.5,
                // skin: 'content-info-layer',// 按钮有样式必须要定义skin
                title: "上传图片",
                content: $('#upload_file_tpl').html(),
                buttons: layerButtons,
                success: function () {
                    //执行实例
                    var uploadInst = upload.render({
                        elem: '#upload_file_btn' //绑定元素
                        , url: '/api/personal/uploadArticleFile?article_id='+ article_id //上传接口
                        , headers: { "token": app.base.getUserInfo().token }
                        , done: function (res) {
                            //上传完毕回调
                            // upload_file_click_url
                            if(res.code==0){
                                $("#upload_file_url").val(res.data)
                            }else{
                                layer.msg("上传失败，请重试")
                            }
                        }
                        , error: function () {
                            //请求异常回调
                            layer.msg("上传失败，请重试")
                        }
                    });
                    // $("#upload_file_btn").click(function(){

                    // })
                }
            })
        }

        // 拦截关闭
        window.addEventListener("beforeunload", function (e) {
            if (isSave==false) {
                var confirmationMessage = "内容有更改没有保存！你确定要离开吗？";
                (e || window.event).returnValue = confirmationMessage; // 兼容 Gecko + IE
                return confirmationMessage; // 兼容 Gecko + Webkit, Safari, Chrome
            }
        });
    })

</script>